package com.jams.goodox.activity.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jams.goodox.BaseFragment;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.adapter.OnlineShoppingCommentAdapter;
import com.jams.goodox.entity.OnlineShoppingCommentBean;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.HomeService;
import com.jams.goodox.widget.NoScrollGridView;
import com.jams.goodox.widget.mannger.RefreshLinearLayoutManager;
import com.jams.library.commonAdapter.ListCommonAdapter;
import com.jams.library.commonAdapter.ListCommonViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;

/**
 * Created by Administrator on 2017/10/4.
 */

public class CommentFragment extends BaseFragment {

    @Bind(R.id.tx_good_number)
    TextView txGoodNumber;
    @Bind(R.id.img_right_arrow)
    ImageView imgRightArrow;
    @Bind(R.id.tx_good_icon)
    TextView txGoodIcon;
    @Bind(R.id.relative_good_right)
    RelativeLayout relativeGoodRight;
    @Bind(R.id.comment_number_grid)
    NoScrollGridView commentNumberGrid;
    @Bind(R.id.recycle_view)
    RecyclerView recycleView;
    private View rootView;

    private ListCommonAdapter listCommonAdapter;
    private List<CommentBean> goodDetailBeanList;
    private List<OnlineShoppingCommentBean> data;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null)
            rootView = inflater.inflate(R.layout.comment_view, container, false);
        ButterKnife.bind(this, rootView);
        init();
        initViews();
        getGoodDetail(getArguments().getString("id"));
        return rootView;
    }

    private void getGoodDetail(String id) {
        enqueue(createCallService(HomeService.class).getProductDetail(SePref.getToken(), id, ""), new RetrofitCallbackListener<GoodsBean>() {
            @Override
            public void onSuccess(Call<GoodsBean> call, GoodsBean result) {
                if (result.getData() == null || result.getData().getInfo() == null || result.getData().getComment() == null)
                    return;
                GoodsBean.Comment comment = result.getData().getComment();
                goodDetailBeanList.clear();
                goodDetailBeanList.add(new CommentBean("好评", comment.getType1()));
                goodDetailBeanList.add(new CommentBean("中评", comment.getType2()));
                goodDetailBeanList.add(new CommentBean("差评", comment.getType3()));
                goodDetailBeanList.add(new CommentBean("五星", comment.getType4()));
                goodDetailBeanList.add(new CommentBean("四星", comment.getType5()));
                listCommonAdapter.notifyDataSetChanged();
                data.clear();
                data.addAll(comment.getItem());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(String msg) {

            }
        });
    }

    private void init() {
        goodDetailBeanList = new ArrayList<>();
        listCommonAdapter = new ListCommonAdapter<CommentBean>(getActivity(), goodDetailBeanList, R.layout.comment_text_item) {
            @Override
            public void convert(ListCommonViewHolder helper, CommentBean item, int position) {
                helper.setText(R.id.tx_comment_number, String.format("%s(%s)", item.getCommentName(), item.getCommentNumber()));
            }
        };
        commentNumberGrid.setAdapter(listCommonAdapter);
    }
    OnlineShoppingCommentAdapter adapter;
    private void initViews() {
        recycleView.setLayoutManager(new RefreshLinearLayoutManager(getActivity()));
        data = new ArrayList<>();
        adapter = new OnlineShoppingCommentAdapter(getActivity(), data);
        recycleView.setAdapter(adapter);
    }
}
